package teamgodeater.hicarnet.MVP.Ui.Main;

import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.lapism.searchview.SearchItem;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import teamgodeater.hicarnet.Adapter.BaseItem2LineAdapter;
import teamgodeater.hicarnet.C;
import teamgodeater.hicarnet.Data.BaseItem2LineData;
import teamgodeater.hicarnet.Data.UserCarInfoData;
import teamgodeater.hicarnet.Help.RestClientHelp;
import teamgodeater.hicarnet.Help.UserDataHelp;
import teamgodeater.hicarnet.MVP.Base.BaseFragmentManage;
import teamgodeater.hicarnet.MVP.Ui.Car.CarFragment;
import teamgodeater.hicarnet.MVP.Ui.Login.LoginFragment;
import teamgodeater.hicarnet.MVP.Ui.WeiZhang.WeizhangFragment;
import teamgodeater.hicarnet.MapHelp.RouteHelp;
import teamgodeater.hicarnet.MapHelp.RoutePlanSearchHelp;
import teamgodeater.hicarnet.R;
import teamgodeater.hicarnet.Utils.Utils;

/**
 * Created by G on 2016/6/24 0024.
 */

public class OverViewPresent extends MainContractor.OverViewPresent implements RoutePlanSearchHelp.OnDrivingRouteListener, BaseItem2LineAdapter.OnItemClickListener {
    MainPresent mDspPresenter;
    private RoutePlanSearchHelp routePlanSearchHelp;


    public void addToPager() {
        BaseItem2LineData routeData = getRouteData();
        List<BaseItem2LineData> overWatchData = getOverWatchData();
        overWatchData.add(0, routeData);
        BaseItem2LineAdapter adapter = new BaseItem2LineAdapter(overWatchData);
        adapter.setOnClickListener(this);
        mView.setFirstViewPager(adapter);
    }

    public BaseItem2LineData getRouteData() {
        BaseItem2LineData data;
        //获取信息失败
        data = new BaseItem2LineData();
        data.icoLeft = R.drawable.ic_traffic;
        data.title = "获取路况数据失败";
        data.tip = "点击刷新";
        data.tipRight = "刷新";
        data.icoRight = R.drawable.ic_keyboard_arrow_right;
        data.tag = "获取路况数据失败";
        if (UserDataHelp.userPointData != null && UserDataHelp.userPointData.getLatitude() == 0) {
            data = new BaseItem2LineData();
            data.icoLeft = R.drawable.ic_traffic;
            data.title = "让我更懂你";
            data.tip = "设置常用路线 避免拥堵";
            data.tipRight = "去设置";
            data.icoRight = R.drawable.ic_keyboard_arrow_right;
            data.tag = "没有设置路况";
        } else  if (UserDataHelp.userTrafficLine != null && UserDataHelp.userTrafficLine.getRouteLines() != null){
            //成功获取
            int maxTraffic = 0;
            for (DrivingRouteLine line : UserDataHelp.userTrafficLine.getRouteLines()) {
                int trafficTime = RouteHelp.getTrafficDis(line);
                if (trafficTime > maxTraffic) {
                    maxTraffic = trafficTime;
                }
            }
            //如果没有拥堵 不显示
            if (maxTraffic > 10) {
                data = new BaseItem2LineData();
                data.icoLeft = R.drawable.ic_traffic;
                data.title = "提示点路线 出现拥堵";
                String dis = "";
                if (maxTraffic < 1000) {
                    dis = maxTraffic + " m";
                } else {
                    DecimalFormat format = new DecimalFormat("#.0");
                    dis = format.format((maxTraffic / 1000f)) + " Km";
                }
                data.tip = "请尽量避免,拥堵 " + dis;
                data.icoRight = R.drawable.ic_keyboard_arrow_right;
                data.tipRight = "查看";
                data.tag = "查看";
            } else {
                data = new BaseItem2LineData();
                data.icoLeft = R.drawable.ic_traffic;
                data.title = "路线畅通";
                data.tip = "拥堵距离小于10m";
                data.tipRight = "查看";
                data.icoRight = R.drawable.ic_keyboard_arrow_right;
                data.tag = "查看";
            }
        }
        data.hasDivider = true;
        return data;
    }

    public List<BaseItem2LineData> getOverWatchData() {
        List<BaseItem2LineData> dataList = new ArrayList<>();
        UserCarInfoData userCarInfoData = UserDataHelp.getDefaultCarInfoData();
        if (userCarInfoData == null) {
            // 获取数据失败
            BaseItem2LineData data = new BaseItem2LineData();
            data.icoLeft = R.drawable.ic_directions_car;
            data.title = "没有添加车辆";
            data.tip = "轻松查看车况 和 违章";
            data.tipRight = "刷新";
            data.icoRight = R.drawable.ic_keyboard_arrow_right;
            data.hasDivider = true;
            data.tag = "获取车辆数据失败";
            dataList.add(data);
        } else if (userCarInfoData.getLicense_num().equals("")) {
            BaseItem2LineData data = new BaseItem2LineData();
            data.icoLeft = R.drawable.ic_directions_car;
            data.title = "没有添加车辆";
            data.tip = "轻松查看车况 和 违章";
            data.tipRight = "去添加";
            data.icoRight = R.drawable.ic_keyboard_arrow_right;
            data.hasDivider = true;
            data.tag = "没有设置车辆";
            dataList.add(data);
        } else {
            //成功获取
            BaseItem2LineData data = new BaseItem2LineData();
            data.icoLeftBitmap = userCarInfoData.getSignBitmap();
            data.title = userCarInfoData.getBrand();
            data.tip = userCarInfoData.getLicense_num();
            data.icoRight = R.drawable.ic_keyboard_arrow_right;
            data.tipRight = "管理车辆";
            data.icoLeft = R.drawable.ic_directions_car;
            data.tag = "管理车辆";
            dataList.add(data);
            //其他违章什么的XXX
            BaseItem2LineData weizhang = new BaseItem2LineData();
            weizhang.title = "无违章记录";
            weizhang.tip = "继续保持下去哦~";
            weizhang.icoRight = R.drawable.ic_keyboard_arrow_right;
            weizhang.tipRight = "违章查询";
            weizhang.tag = "违章查询";
            dataList.add(weizhang);
            //性能
            BaseItem2LineData performance = new BaseItem2LineData();
            performance.hasDivider = true;
            if (userCarInfoData.getEngine_performance() == 2 || userCarInfoData.getTransmission_performance() == 2
                    || userCarInfoData.getLight_performance() == 2) {
                performance.title = "车辆出现故障";
                String guzhang = "";
                if (userCarInfoData.getEngine_performance() == 2) {
                    guzhang += "发动机 ";
                }
                if (userCarInfoData.getLight_performance() == 2) {
                    guzhang += "车灯 ";
                }
                if (userCarInfoData.getTransmission_performance() == 2) {
                    guzhang += "变速器 ";
                }
                performance.tip = guzhang + "出现异常";
                performance.tipRight = "附近的4S店";
                performance.icoRight = R.drawable.ic_keyboard_arrow_right;
                performance.tag = "附近的4S店";
            } else {
                performance.title = "没有异常";
                performance.tip = "发动机 车灯 变速器 正常";
                performance.isClickAble = false;
            }
            dataList.add(performance);
        }
        return dataList;
    }

    @Override
    protected void onStart() {
        mDspPresenter = mView.mPresenter;
        routePlanSearchHelp = new RoutePlanSearchHelp(mView.manageActivity, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDspPresenter = null;
        routePlanSearchHelp.onDestroy();
    }

    @Override
    public void onSucceed(DrivingRouteResult route) {

    }

    @Override
    public void onErrorRoute(int code) {

    }

    /**
     * adapter项目点击
     *
     * @param data
     * @param position
     */
    @Override
    public void onClick(BaseItem2LineData data, int position) {
        String tag = (String) data.tag;
        if (tag.equals("获取路况数据失败")) {
            getTraffic();
        } else if (tag.equals("没有设置路况")) {
            addTraffic();
        } else if (tag.equals("查看")) {
            chaKan();
        } else if (tag.equals("获取车辆数据失败")) {
            getCarDatas();
        } else if (tag.equals("没有设置车辆")) {
            toSetCar();
        } else if (tag.equals("违章查询")) {
            toWeizhang();
        } else if (tag.equals("附近的4S店")) {
            fourS();
        } else if (tag.equals("路线畅通")) {
            getTraffic();
        } else if (tag.equals("管理车辆")) {
            mView.hideSelf(400L);
            BaseFragmentManage.switchFragment(CarFragment.getInstans());
        }
    }

    private void addTraffic() {
        mDspPresenter.trafficPointPresent.start(true);
    }

    private void chaKan() {
        SearchItem searchItem = new SearchItem();
        searchItem.key = UserDataHelp.userPointData.getComment() + " 路况信息";
        searchItem.latitude = UserDataHelp.userPointData.getLatitude();
        searchItem.longitude = UserDataHelp.userPointData.getLongitude();
        mDspPresenter.trafficPointPresent.start(false);
        mDspPresenter.mapSearchPresent.showRoute(searchItem);
    }

    private void fourS() {

    }

    private void getTraffic() {
        mView.showPagerLoading("正在加载路况数据...");
        UserDataHelp.getUserPointData(
                new UserDataHelp.OnDoneListener() {
                    @Override
                    public void onDone(int code) {
                        mView.hidePagerLoading();
                        if (code == 200) {
                            addToPager();
                            chaKan();
                        } else if (code == C.HTTP_NOT_FOUND) {
                            addTraffic();
                        } else if (code == C.HTTP_UNAUTHORIZED) {
                            Utils.toast("你还没有登录哦 请先登录");
                            mView.hideSelf(400L);
                            BaseFragmentManage.switchFragment(new LoginFragment());
                        } else
                            RestClientHelp.generalErrorToast(code);
                    }
                }
        );
    }

    private void toWeizhang() {
        mView.hideSelf(400L);
        BaseFragmentManage.switchFragment(WeizhangFragment.getInstans());
    }

    private void toSetCar() {
        Utils.toast("点击 + 来添加你的车量信息");
        mView.hideSelf(400L);
        BaseFragmentManage.switchFragment(CarFragment.getInstans());
    }

    private void getCarDatas() {
        mView.showPagerLoading("正在获取,车辆数据 请稍后...");
        UserDataHelp.getUserCarInfoDatas(new UserDataHelp.OnDoneListener() {
            @Override
            public void onDone(int code) {
                mView.hidePagerLoading();
                if (code == C.HTTP_UNAUTHORIZED) {
                    Utils.toast("你还没有登录哦 请先登录");
                    mView.hideSelf(400L);
                    BaseFragmentManage.switchFragment(new LoginFragment());
                } else if (code == C.HTTP_NOT_FOUND) {
                    toSetCar();
                } else {
                }
                addToPager();
            }
        });
    }
}
